//
//  ZH_TeacherViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/19.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_TeacherViewController.h"
#import "ZH_TeacherTableViewCell.h"
#import "ZH_TeacherInfomationViewController.h"
#import "ZH_LookTeacherViewModel.h"
@interface ZH_TeacherViewController ()<UITableViewDelegate,UITableViewDataSource>
//tableView
@property (nonatomic,weak)UITableView *tableView;
//viewModel
@property (nonatomic,strong)ZH_LookTeacherViewModel *viewModel;
@end

@implementation ZH_TeacherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    //找老师
    [self initTeacherData];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initUserInterface{
    self.view.backgroundColor = kCommonWhiteBg;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(FITSCALE(9));
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-[DATAMODEL KNavigationBarHeioght]);
    }];
}
#pragma mark - initTeacherData
- (void)initTeacherData{
    [self.viewModel.lookTeacherParam setValue:self.latString forKey:@"lat"];
    [self.viewModel.lookTeacherParam setValue:self.lngString forKey:@"lng"];
    [self.viewModel.lookTeacherParam setValue:self.cateString forKey:@"cate"];
    WEAKSelf;
    [self.viewModel lookTeacherResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZH_TeacherTableViewCell *cell = [ZH_TeacherTableViewCell cellWithTableView:tableView];
    [cell updateUiForData:self.viewModel.lookTeacherArray];
    WEAKSelf;
    cell.onClickBlock = ^(NSInteger index) {
        ZH_TeacherInfomationViewController *vc = [[ZH_TeacherInfomationViewController alloc] init];
        vc.typeString = @"1";
        vc.idString = [NSString stringWithFormat:@"%@",self.viewModel.lookTeacherArray[index][@"id"]];
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    return cell;
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonWhiteBg;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_LookTeacherViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_LookTeacherViewModel alloc] init];
    }
    return _viewModel;
}
@end
